<?php

  if($DownloadMailbox == '1')
    {

      // Create Object For reciveMail Class
      //$obj= new receiveMail('username','password','email','server','servertype','port');
      $obj= new receiveMail($mailboxUsername,$mailboxPassword,$mailboxEmail,$mailboxServer,$mailboxServerType,$mailboxServerPort);
      //Connect to the Mail Box
      $obj->connect();
      // Get Total Number of Unread Email in mail box
      $tot=$obj->getTotalMails(); //Total Mails in Inbox Return integer value

      if($tot > 0)
        {

          for($i=1;$i<=$tot;$i++)
          {
            $head=$obj->getHeaders($i);  // Get Header Info Return Array Of Headers **Key Are (subject,to,toOth,toNameOth,from,fromName)

            $from_name = $head['fromName']; // Get the From Name
            $from_email = $head['from']; // Get the From Address
            $subject = $head['subject']; // Get the Subject
            $message = $obj->getBody($i); // Get the Email Message
            $download_time = date("Y-m-d H:i:s"); // time of hit

            if(($message)OR($subject))
              {

                if(checkBanList($from_email) != '0')
                  {

                      // Convert of Subject
                      $subject = htmlentities($subject, ENT_QUOTES);

                      // Convert of Message and get rid of unwanted tags etc
                      $message = str_replace("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">", "", $message);
                      $message = str_replace("<HTML>", "", $message);
                      $message = str_replace("</HTML>", "", $message);
                      $message = str_replace("<BODY>", "", $message);
                      $message = str_replace("</BODY>", "", $message);
                      $message = preg_replace("@<HEAD[^>]*?>.*?</HEAD>@siu", "", $message);
                      $message = preg_replace("@<head[^>]*?>.*?</head>@siu", "", $message);
                      $message = htmlentities($message, ENT_QUOTES);

                      // Insert our Mailbox Item into the Databas
                      $query_add = "INSERT INTO `orbitfaq_mailbox` (
                          `downloaded`,
                          `from_name`,
                          `from_email`,
                          `subject`,
                          `message`,
                          `status`
                        )VALUES(
                          '$download_time',
                          '$from_name',
                          '$from_email',
                          '$subject',
                          '$message',
                          '0'
                        );

                      ";
                      $result_add = $faqsql_query ($query_add)OR DIE( "$sql_query_error $query_add");
                  }

              }

            $obj->deleteMails($i); // Delete Mail from Mail box
          }
          $obj->close_mailbox();   //Close Mail Box

      }

  }

?>